<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use app\common\model\NumberCardItem;
use think\Db;
use think\Model as ThinkModel;

//银行列表模型
class NumberCardTemplate extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_VIPER_NUMBER_CARD_TEMPLATE__';
    protected $name = 'qt_viper_number_card_template';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * 创建次卡模版
     * @param $arr
     * @param $store_id
     * @param $viper_type_id
     */
    public static function createByTemplate($arr,$store_id,$viper_type_id)
    {
        foreach ($arr as $key=>$item)
        {
            if(!isset($item['rate'])){
                $item['rate'] = '0';
            }
            $template = self::create([
                'name' => $item['name'],
                'amount' => $item['amount'],
                'rate' => $item['rate'],
                'store_id' => $store_id,
                'viper_type_id' => $viper_type_id,
            ]);
            $timeData=  json_decode($item['item'],true);
            foreach ($timeData as $k=> $i)
            {
                $collectionId = Db::name('qt_viper_number_card_template_collection')->insertGetId([
                    'number_template_id'=>$template['id'],
                    'data'=>json_encode($i['data']) ,
                    'number'=>$i['number'],
                    'expire'=>$i['expire'],
                    'name'=>json_encode($i['name']),
                    'create_time'=>time(),
                ]);
                if(!empty($i['data'])){
                    foreach ($i['data'] as $kk=>$v){
                        $goods = explode('-',$v);
                        NumberCardItem::create([
                            'number'=>$i['number'],
                            'expire'=>$i['expire'],
                            'item_id'=>$goods[1],
                            'item_type'=>$goods[0],
                            'store_id'=>$store_id,
                            'number_card_template_id'=>$template['id'],
                            'number_card_template_collection_id'=>$collectionId,
                            'viper_type_id'=>$viper_type_id,
                        ]);
                    }
                }
            }
        }
    }

    /**
     * 获取门店会员类型的次卡模版
     * @param $store_id
     * @param $viper_type_id
     * @return NumberCardTemplate[]|\think\Collection
     */
    public static function getTemplateOfStore($store_id,$viper_type_id)
    {
        $list = self::where(['store_id'=>$store_id,'viper_type_id'=>$viper_type_id])
            ->field('name,amount,rate,id')
            ->select();

        foreach ($list as $k=>$item)
        {
            $items = Db::name('qt_viper_number_card_template_collection')->where('number_template_id',$item['id'])->select();
            foreach ($items as $key=>$value ){
                $items[$key]['data'] = json_decode($value['data'],true);
                $items[$key]['name'] = json_decode($value['name'],true);
            }
            $item['item'] = $items;

            $list[$k] = $item;
        }

        return $list;
    }


    /**
     * 补录会员卡次卡添加
     * @param $arr
     * @param $store_id
     * @param $viper_type_id
     */
    public static function supplementCreateByTemplate($arr,$userInfo,$viper_type_id,$viper_card_id)
    {
        $arr = json_decode($arr,true);
        foreach ($arr as $key=>$item)
        {
            if(!isset($item['rate'])){
                $item['rate'] = '0';
            }
            $template = self::create([
                'name' => $item['name'],
                'expire' => $item['expire'],
                'store_id' => $userInfo['store_id'],
                'viper_type_id' => $viper_type_id,
            ]);
            foreach ($item['item'] as $k=> $v)
            {
                $goods = explode('-',$v);
                NumberCardItem::create([
                    'number'=>$item['number'],
                    'expire'=>$item['expire'],
                    'item_id'=>$goods[1],
                    'item_type'=>$goods[0],
                    'store_id'=>$userInfo['store_id'],
                    'number_card_template_id'=>$template['id'],
                    'viper_type_id'=>$viper_type_id,
                ]);
            }
            $card_item = [
                'viper_card_id' => $viper_card_id,
                'viper_type_id' => $viper_type_id,
                'number_template_id' => $template['id'],
                'status' => 1,
                'total_number' => $item['number'],
                'use_number' => 0,
            ];
            Db::name('qt_viper_card_item')->insert($card_item);
            //写充值记录
            $supplement = [
                'viper_card_id' => $viper_card_id,
                'viper_type_id' => $viper_type_id,
                'template_id' => $template['id'],
                'business_type' => 1,
                'creater' => $userInfo['id'],
                'create_time' => time(),
            ];
            Db::name('qt_viper_card_number_supplement')->insert($supplement);

        }
    }


    /**
     * 更新次卡模版
     * @param $arr
     * @param $store_id
     * @param $viper_type_id
     */
    public static function updateByTemplate($arr,$store_id,$viper_type_id)
    {
        foreach ($arr as $key=>$item)
        {
            if(!isset($item['rate'])){
                $item['rate'] = '0';
            }
            //dump($item);die;
            if(isset($item['id']) && !empty($item['id'])){
                self::where(['id'=>$item['id']])->update([
                    'name' => $item['name'],
                    'amount' => $item['amount'],
                    'rate' => $item['rate'],
                    'store_id' => $store_id,
                    'viper_type_id' => $viper_type_id,
                ]);
                $templateId = $item['id'];
            }else{
                $template = self::create([
                    'name' => $item['name'],
                    'amount' => $item['amount'],
                    'rate' => $item['rate'],
                    'store_id' => $store_id,
                    'viper_type_id' => $viper_type_id,
                ]);
                $templateId = $template->id;
            }

            $timeData=  json_decode($item['item'],true);
            foreach ($timeData as $k=> $i)
            {
                $collectionId = Db::name('qt_viper_number_card_template_collection')->insertGetId([
                    'number_template_id'=>$templateId,
                    'data'=>json_encode($i['data']) ,
                    'number'=>$i['number'],
                    'expire'=>$i['expire'],
                    'name'=>json_encode($i['name']),
                    'create_time'=>time(),
                ]);
                if(!empty($i['data'])){
                    Db::name('qt_viper_number_card_template_item')->where(['number_card_template_id'=>$templateId])->delete();
                    foreach ($i['data'] as $kk=>$v){
                        $goods = explode('-',$v);
                        NumberCardItem::create([
                            'number'=>$i['number'],
                            'expire'=>$i['expire'],
                            'item_id'=>$goods[1],
                            'item_type'=>$goods[0],
                            'store_id'=>$store_id,
                            'number_card_template_id'=>$templateId,
                            'number_card_template_collection_id'=>$collectionId,
                            'viper_type_id'=>$viper_type_id,
                        ]);
                    }
                }
            }
        }
    }



}